package com.example.dbcms.utils;

import com.example.dbcms.service.UserService;
import com.example.dbcms.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * 文件上传工具类
 */
public class FileUploadUtil {
    /**
     * 图片上传
     *
     * @param file      图片文件前端传过来
     * @param file_path 文件路径
     * @return
     */
    public static ResultVo pictureUpload(MultipartFile file, String file_path,String uuid) {
        if (file.isEmpty()) {
            return ResultVoUtil.error("文件为空");
        }
        //文件名
        String fileName = file.getOriginalFilename();
        //获取后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //上传路径
        String filePath = file_path;
        //新文件名
        fileName = uuid + suffixName;
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVoUtil.success(fileName);
    }
}
